package com.olocat.cjx.nsims.controller

import com.alibaba.fastjson.JSON
import com.olocat.cjx.nsims.pojo.personnel.Ward
import com.olocat.cjx.nsims.service.WardService
import com.olocat.cjx.nsims.util.Response
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource


@RestController
class WardController {
    @Resource
    lateinit var wardService: WardService

    @GetMapping("/api/ward/get")
    fun getBed(ward:String?, department:Int?): Response {
        println("获取床位列表: $ward")
        return if (ward == null){
            if(department == null)
                Response.builder(wardService.list())
            else
                Response.builder(wardService.getWardsByDepartment(department))
        }else{
             Response.builder(wardService.getById(ward))
        }
    }
    @GetMapping("/api/ward/doctor")
    fun getWardByDoctor(doctor:String):Response{
        return Response.builder(wardService.getWardsByDoctor(doctor))
    }
    @PostMapping("/api/ward/add")
    fun add(@RequestBody json:String) : Response {
        val ward = JSON.parseObject(json, Ward::class.java)
        wardService.save(ward)
        return Response.builder()
    }

    @PostMapping("/api/ward/update")
    fun update(@RequestBody json:String) : Response {
        val ward = JSON.parseObject(json, Ward::class.java)
        wardService.save(ward)
        return Response.builder()
    }

    @DeleteMapping("api/ward/del")
    fun del(number:String) : Response {
        wardService.removeByNumber(number)
        return Response.builder()
    }

    @GetMapping("/api/ward/patient/list")
    fun getBedPatientList(ward:String) : Response {
        println("ward: $ward")
        val re = wardService.listBedPatient(ward)
        println(re)
        return Response.builder(re)
    }
}